<?php
namespace addons\hmpt\library;

use hema\delivery\engine\Basics;

/**
 * 河马跑腿
*/
class Hmpt extends Basics
{
    private $open_version = 'v1';//开放接口版本
    
	/**
     * 预发布订单创建
	*/
	public function preOrder($data)
	{
	    $params = $this->params();
	    $params['body'] = json_encode($data,JSON_UNESCAPED_UNICODE);
		$result = $this->post($params, "order/precreate");
		if($result['code'] == 0){
			return [
			    'delivery_no' => $result['data']['order_no'], //配送单号
				'delivery_distance' => $result['data']['distance']['text'],//配送距离
				'delivery_time' => $result['data']['estimate_time']['text'],//预计送达时间
				'delivery_price' => $result['data']['pay_price'],//配送费用
				'time' => $result['data']['estimate_time']['value'],//预计送达时间
				'distance' => $result['data']['distance']['value'],//配送距离
				'hmpt' => $result['data']
			];
		}
		$this->error = $result['msg'];
		return false;
	}
	
	/**
     * 创建订单
	*/
	public function addOrder($data)
	{
		$params = $this->params();
	    $params['body'] = json_encode($data,JSON_UNESCAPED_UNICODE);
		$result = $this->post($params, "order/add");
		if($result['code'] == 0){
			return $result['data'];
		}
		$this->error = $result['msg'];
		return false;
	}

	/**
     * 取消订单
	*/
	public function cancelOrder($data)
	{
	    $params = $this->params();
	    $params['body'] = json_encode($data,JSON_UNESCAPED_UNICODE);
		$result = $this->post($params, "order/cancel");
		if($result['code'] == 0){
			return $result['data'];
		}
		$this->error = $result['msg'];
		return false;
	}
	
	/**
     * 获取骑手实时位置
	*/
	public function goodsAttribute()
	{
	    $params = $this->params();
	    $params['body'] = json_encode([],JSON_UNESCAPED_UNICODE);
		$result = $this->post($params, "goods/attribute");
		if($result['code'] == 0){
			return $result['data'];
		}
		$this->error = $result['msg'];
		return false;
	}
	/**
     * 获取骑手实时位置
	*/
	public function riderPosition($data)
	{
	    $params = $this->params();
	    $params['body'] = json_encode($data,JSON_UNESCAPED_UNICODE);
		$result = $this->post($params, "rider/position");
		if($result['code'] == 0){
			return $result['data'];
		}
		$this->error = $result['msg'];
		return false;
	}
    
    /**
     * 获取门店列表
	*/
	public function shopLists($search='',$page=true)
	{
	    $data = [
	        'search' => $search,
	        'page' => $page,
	    ];
	    $params = $this->params();
	    $params['body'] = json_encode($data,JSON_UNESCAPED_UNICODE);
		$result = $this->post($params, "shop/lists");
		if($result['code'] == 0){
			return $result['data'];
		}
		$this->error = $result['msg'];
		return false;
	}
	/**
     * 获取门店详情
	*/
	public function shopDetail($shop_id)
	{
	    $data = ['shop_id' => $shop_id];
	    $params = $this->params();
	    $params['body'] = json_encode($data,JSON_UNESCAPED_UNICODE);
		$result = $this->post($params, "shop/detail");
		if($result['code'] == 0){
			return $result['data'];
		}
		$this->error = $result['msg'];
		return false;
	}
	
	/**
     * 添加门店
	*/
	public function shopAdd($data)
	{
	    $params = $this->params();
	    $params['body'] = json_encode($data,JSON_UNESCAPED_UNICODE);
		$result = $this->post($params, "shop/add");
		if($result['code'] == 0){
			return $result['data'];
		}
		$this->error = $result['msg'];
		return false;
	}
	
	/**
     * 编辑门店
	*/
	public function shopEdit($data)
	{
	    $params = $this->params();
	    $params['body'] = json_encode($data,JSON_UNESCAPED_UNICODE);
		$result = $this->post($params, "shop/edit");
		if($result['code'] == 0){
			return $result['data'];
		}
		$this->error = $result['msg'];
		return false;
	}
	
	/**
     * 删除门店
	*/
	public function shopDelete($shop_id)
	{
	    $data = ['shop_id' => $shop_id];
	    $params = $this->params();
	    $params['body'] = json_encode($data,JSON_UNESCAPED_UNICODE);
		$result = $this->post($params, "shop/delete");
		if($result['code'] == 0){
			return $result['data'];
		}
		$this->error = $result['msg'];
		return false;
	}
	
    /**
    * 公共参数
    */
    private function params(){
        $time = time();         //请求时间
        return $params = [
            'dev_id' => $this->config['dev_id'],
            'timestamp' => $time,
            'sign' => $this->_sign($time)
        ];
    }
    
    /**
    * 生成签名
    */
    private function _sign($timestamp)
    {
        return md5($this->config['dev_id'] . $timestamp . $this->config['dev_secret']);
    }

    /**
     * 发送请求,POST
     * @param $url 指定URL完整路径地址
     * @param $params 请求的数据
     */
    private function post($params,$url)
	{
		$params = json_encode($params,JSON_UNESCAPED_UNICODE);
        $url = $this->config['api_url'] . '/api/' . $this->open_version . '.' . $url;
		// json
        $headers = array(
            'Content-Type: application/json',
        );
        $curl = curl_init();
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//https
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);//https
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);//POST请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $params);//post参数
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 3);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $resp = curl_exec($curl);
        //var_dump( curl_error($curl) );//如果在执行curl的过程中出现异常，可以打开此开关查看异常内容。
        $info = curl_getinfo($curl);
        curl_close($curl);
        if (isset($info['http_code']) && $info['http_code'] == 200) {
            return json_decode($resp, true);
        }
        return false;
    } 
}
